﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ECare.BUS.Data;
using ECare.Database;

namespace ECare.BUS
{
    public class ResourceSupplyHelper
    {

        public static int CreateResourceSupply(ResourceSupplyInfo info)
        {
            try
            {
                using (var context = new Entities())
                {
                    var newinfo = new resource_supply();
                    newinfo.resource_supply_code = info.resource_supply_code;
                    newinfo.resource_supply_name = info.resource_supply_name;
                    newinfo.resource_supply_un = info.resource_supply_un;
                    newinfo.resource_supply_phone = info.resource_supply_phone;
                    newinfo.resource_supply_email = info.resource_supply_email;
                    newinfo.resource_supply_address = info.resource_supply_address;

                    context.resource_supply.AddObject(newinfo);
                    context.SaveChanges();
                    return newinfo.resource_supply_id;
                }
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public static List<ResourceSupplyInfo> GetResources()
        {
            using (var context = new Entities())
            {
                var res = new List<ResourceSupplyInfo>();

                var resources = context.resource_supply;
                foreach (var resource in resources)
                {
                    var info = new ResourceSupplyInfo();
                    info.resource_supply_address = resource.resource_supply_address;
                    info.resource_supply_code = resource.resource_supply_code;
                    info.resource_supply_email = resource.resource_supply_email;
                    info.resource_supply_id = resource.resource_supply_id;
                    info.resource_supply_name = resource.resource_supply_name;
                    info.resource_supply_phone = resource.resource_supply_phone;
                    info.resource_supply_un = resource.resource_supply_un;

                    res.Add(info);
                }
                return res;
            }
        }


        public static string GetSupplyNameById(int Id)
        {
            using (var context = new Entities())
            {
                var findsupply = context.resource_supply.FirstOrDefault(a => a.resource_supply_id == Id);
                if (findsupply == null)
                    return "未知供应商";
                else
                {
                    return findsupply.resource_supply_name;
                }
            }
        }

        public static bool UpdateResourceInfo(ResourceSupplyInfo info)
        {

            using (var context = new Entities())
            {
                var findresult = context.resource_supply.FirstOrDefault(a => a.resource_supply_id == info.resource_supply_id);

                if (findresult == null)
                    return false;
                findresult.resource_supply_code = info.resource_supply_code;
                findresult.resource_supply_name = info.resource_supply_name;
                findresult.resource_supply_un = info.resource_supply_un;
                findresult.resource_supply_phone = info.resource_supply_phone;
                findresult.resource_supply_email = info.resource_supply_email;
                findresult.resource_supply_address = info.resource_supply_address;

                context.SaveChanges();
            }
            return false;
        }

        public static bool CheckResourceSupplyName(ResourceSupplyInfo resourcesupplyinfo)
        {
            try
            {
                using (var context = new Entities())
                {
                    var findresourcesupply = context.resource_supply.FirstOrDefault(a => (a.resource_supply_name == resourcesupplyinfo.resource_supply_name) || (a.resource_supply_code == resourcesupplyinfo.resource_supply_code));
                    
                    if (findresourcesupply == null)
                        return true;
                    else
                        return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        
        }
    }
}
